$(function() {
    // jquery.validate 自定义校验
    jQuery.validator.addMethod("myValid01", function(value, element) {
        var length = value.length;
        var valid = /^[a-z][a-z0-9-]*$/;
        return this.optional(element) || valid.test(value);
    }, "限制以小写字母开头，由小写字母、数字和中划线组成");

    $('.update').on('click', function(){
        $("#updateModal .form input[name='id']").val($(this).attr("thisId"));
        $("#updateModal .form input[name='app']").val($(this).attr("thisApp"));
        $("#updateModal .form input[name='weight']").val($(this).attr("thisEnv"));
        $("#updateModal .form select[name='status']").find("option[value='"+ $(this).attr("thisStatus") +"']").prop("selected", 'selected');

        $('#updateModal').modal({backdrop: false, keyboard: false}).modal('show');
    });
    var updateModalValidate = $("#updateModal .form").validate({
        errorElement : 'span',
        errorClass : 'help-block',
        focusInvalid : true,
        rules : {
            weight : {
                required : true,
                rangelength:[1, 12]
            }
        },
        messages : {
            weight : {
                required :"请输入权重",
                rangelength:"长度限制为1~12"
            }
        },
        highlight : function(element) {
            $(element).closest('.form-group').addClass('has-error');
        },
        success : function(label) {
            label.closest('.form-group').removeClass('has-error');
            label.remove();
        },
        errorPlacement : function(error, element) {
            element.parent('div').append(error);
        },
        submitHandler : function(form) {
            $.post(base_url + "/server/update",  $("#updateModal .form").serialize(), function(data, status) {
                if (data.code === 200) {
                    $('#addModal').modal('hide');
                    layer.open({
                        icon: '1',
                        content: '更新成功' ,
                        end: function(layero, index){
                            window.location.reload();
                        }
                    });
                } else {
                    layer.open({
                        icon: '2',
                        content: (data.msg||'更新失败')
                    });
                }
            });
        }
    });
    $("#updateModal").on('hide.bs.modal', function () {
        $("#updateModal .form")[0].reset();
        updateModalValidate.resetForm();
        $("#updateModal .form .form-group").removeClass("has-error");
    });

});
